#include <stdio.h>
#include <unistd.h>

class Handler
{
 public:
  Handler(Handler *handler):mSuccessor(handler){};
  virtual ~Handler(){};
  virtual void handleRequest(char c) = 0;
 protected:
  Handler *mSuccessor;
};

class ConcreateHandler1 : public Handler
{
 public:
  ConcreateHandler1(Handler *handler):Handler(handler){};
  void handleRequest(char c);
};
void ConcreateHandler1::handleRequest(char c)
{
  if (c == 'a')
  {
    printf("I can handle \'a\'");
  }
  else if (mSuccessor != NULL)
  {
    mSuccessor->handleRequest(c);
  }
}

class ConcreateHandler2 : public Handler
{
 public:
  ConcreateHandler2(Handler *handler):Handler(handler){};
  void handleRequest(char c);
};
void ConcreateHandler2::handleRequest(char c)
{
  if (c == 'b')
  {
    printf("I can handle \'b\'");
  }
  else if (mSuccessor != NULL)
  {
    mSuccessor->handleRequest(c);
  }
}

int main()
{
  Handler *h2 = new ConcreateHandler2(NULL);
  Handler *h1 = new ConcreateHandler1(h2);
  h1->handleRequest('b');
  delete h1;
  delete h2;

  fflush(stdout);
  sleep(5);
}
